//index.js
//获取应用实例
var app = getApp();
var md5 = require("../../utils/md5.js");
Page({
  data: {
    addressList:[]
  },
  onLoad: function () {
    console.log('onLoad');
  },
  onShow: function () {
    this.initShippingAddress(1);
  },
  	
	initShippingAddress: function (type) {
		var that = this;
		var open_id = app.globalData.openId;
		var service = 'Order.GetNewAddress';
		var user_id = app.globalData.userId;
		var sign = md5.md5(open_id + service + user_id);
    var addressSelectInfo = wx.getStorageSync('addressSelectInfo');
		wx.request({
			url: app.globalData.serviceUrl + 'Order.GetNewAddress',
			header: {
				'Content-Type': 'json'
			},
			data: {
				open_id: open_id,
				user_id: user_id,
				sign: sign,
			},
			success: (res) =>{
				var result = res.data.data;
        var newList = []
				if (result.code == 0) {
          var list = []
          if (result.info) {
            list = result.info
          }

          if (list.length > 0) {
            var isTrue = false
            for (var i = 0; i < list.length; i++) {
              var dic = list[i];
              if (dic.address_id == addressSelectInfo.address_id) {
                dic.is_moren = true
                isTrue = true
              } else {
                dic.is_moren = false
              }
              newList.push(dic)
            }

            if (isTrue == false) {
              var secondList = newList
              newList = []
              for (var i = 0; i < secondList.length; i++) {
                var dic = secondList[i];
                if (i == 0) {
                  dic.is_moren = true
                  wx.setStorageSync("addressSelectInfo", dic);
                } else {
                  dic.is_moren = false
                }
                newList.push(dic)
              }
            }
            that.setData({
              addressList: newList
            });
          } else {
            that.setData({
              addressList: []
            });
            wx.setStorageSync("addressSelectList", false);
            wx.setStorageSync("addressSelectInfo", false);
          }
         
				} else {
          that.setData({
            addressList: []
          });
          wx.setStorageSync("addressSelectList", false);
          wx.setStorageSync("addressSelectInfo", false);
        }
			},
			complete: function(res) {
				console.log(res);
			},
		})
	},

  selectTap: function (e) {
    var addressInfo = {};
    for (var i = 0; i < this.data.addressList.length; i++) {
      if (this.data.addressList[i].address_id == e.currentTarget.dataset.id) {
        addressInfo = this.data.addressList[i];;
        var that = this;
        var open_id = app.globalData.openId;
        var service = 'Order.DefaultAddress';
        var user_id = app.globalData.userId;
        var address_id = addressInfo.address_id;
        var sign = md5.md5(address_id + open_id + service + user_id);
        wx.request({
          url: app.globalData.serviceUrl + 'Order.DefaultAddress',
          header: {
            'Content-Type': 'json'
          },
          data: {
            open_id: open_id,
            user_id: user_id,
            address_id: address_id,
            sign: sign,
          },
          success: (res) => {
            wx.navigateBack({});
            wx.setStorageSync("addressSelectInfo", addressInfo);
          }
        });
      }
    } 
    
  },

  addAddess: function () {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  editAddess: function (e) {
    var addressInfo = {};
    for (var i = 0; i < this.data.addressList.length; i++) {
      if (this.data.addressList[i].address_id == e.currentTarget.dataset.id) {
        addressInfo = this.data.addressList[i]
      }
    }
    wx.setStorageSync("addressInfo", addressInfo);
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },

  deleteAddess: function (e) {
    var that = this;
    wx.showLoading();
    var addressSelectInfo = wx.getStorageSync('addressSelectInfo');
    var address_id = e.currentTarget.dataset.id;
    if (address_id == addressSelectInfo.address_id) {
      wx.setStorageSync("addressSelectInfo", false);
    }
    var open_id = app.globalData.openId;
    var service = 'Order.DelAddress';
    var user_id = app.globalData.userId;
    var sign = md5.md5(address_id + open_id + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + 'Order.DelAddress',
      header: {
        'Content-Type': 'json'
      },
      data: {
        address_id: address_id,
        open_id: open_id,
        user_id: user_id,
        sign: sign
      },
      success: function (res) {
        var result = res.data.data;
        if (result.code != 0) {
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.data.msg,
            showCancel: false
          })
          return;
        }
        wx.hideLoading();
        that.initShippingAddress(2);
      },
      complete: function (res) {
        console.log(res);
      },
    })
  }
})
